unit uFrmCFOP;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, DB, DBAccess, Uni, MemDS, JvExComCtrls, JvDBTreeView,
  StdCtrls, ExtCtrls, JvComCtrls;

type
  TFrmCFOP = class(TForm)
    UniQuery1: TUniQuery;
    UniDataSource1: TUniDataSource;
    Panel1: TPanel;
    EdtPesquisa: TEdit;
    BtnPesquisar: TButton;
    Button1: TButton;
    JvTreeView1: TJvTreeView;
    function NodeExist(Texto : string) : Boolean;
    function RetornaIndexNode(Texto : string) : Integer;
    procedure PopularTreeView;
    procedure FormShow(Sender: TObject);
    procedure PesquisarCFOP;
    function RetornaIndexNodeCFOP(CFOP4Digitos : string) : Integer;
    procedure BtnPesquisarClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
   sPesquisa : string;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmCFOP: TFrmCFOP;

implementation

uses uDM;

{$R *.dfm}

procedure TFrmCFOP.BtnPesquisarClick(Sender: TObject);
begin
  PesquisarCFOP;
end;

procedure TFrmCFOP.Button1Click(Sender: TObject);
var r : TTreeNode;
begin

  {r := JvTreeView1.Items.Item[5];
  R.Selected := True;
  JvTreeView1.SetFocus;

  if r.Focused then ShowMessage('selecionado');
                                                }
end;

procedure TFrmCFOP.FormShow(Sender: TObject);
begin
  PopularTreeView;
end;

function TFrmCFOP.NodeExist(Texto: string): Boolean;
var i : Integer;
begin
  Result := false;
  for i := 0 to JvTreeView1.Items.Count - 1 do
  begin
    if JvTreeView1.Items[i].Text = Texto then
    begin
      Result := True;
      Break;
    end;

  end;
end;

procedure TFrmCFOP.PesquisarCFOP;
var r : TTreeNode;
begin
  if EdtPesquisa.Text <> sPesquisa then
  begin
    with UniQuery1 do
    begin
      Close;
      SQL.Text := 'select t.cfop from tcfop t where  cast((t.cfop || ' + ''' +  - ''' + ' || t.descricao) as VARCHAR(1000) character set ISO8859_1) collate PT_BR like ' + QuotedStr('%' + EdtPesquisa.Text + '%');
      Open;
      if not UniQuery1.IsEmpty then
      begin
        r := JvTreeView1.Items[RetornaIndexNodeCFOP(UniQuery1.FieldByName('CFOP').AsString)];
        r.Selected := True;
        JvTreeView1.SetFocus;
        //EdtPesquisa.SetFocus;
        {JvTreeView1.Items[RetornaIndexNodeCFOP(UniQuery1.FieldByName('CFOP').AsString)].Expanded := True;
        JvTreeView1.Items[RetornaIndexNodeCFOP(UniQuery1.FieldByName('CFOP').AsString)].Selected;}
        //ShowMessage(IntToStr(RetornaIndexNodeCFOP(UniQuery1.FieldByName('CFOP').AsString)));
      end;
    end;
  end
  else
  if not UniQuery1.IsEmpty then
    if not UniQuery1.Eof then
      begin
        UniQuery1.Next;
        r := JvTreeView1.Items[RetornaIndexNodeCFOP(UniQuery1.FieldByName('CFOP').AsString)];
        r.Selected := True;
        JvTreeView1.SetFocus;
        //EdtPesquisa.SetFocus;
        //ShowMessage(IntToStr(RetornaIndexNodeCFOP(UniQuery1.FieldByName('CFOP').AsString)));
      end;
  sPesquisa := EdtPesquisa.Text;
end;

procedure TFrmCFOP.PopularTreeView;
var R , R1 , R2 , R3 , R4 , R5 , R6 , Rtemp   : TTreeNode;
  S , NomeNode: string;
  I : Integer;
begin
  R1 := JvTreeView1.Items[0];
  R2 := JvTreeView1.Items[1];
  R3 := JvTreeView1.Items[2];
  R4 := JvTreeView1.Items[3];
  R5 := JvTreeView1.Items[4];
  R6 := JvTreeView1.Items[5];


{   Root := JvTreeView1.Items[0];
   JvTreeView1.Items.AddChild(Root,'teste');}
   with UniQuery1 do
  begin
    Close;
    SQL.Text := 'select t.cfop ,  (t.cfop || ' + ''' - ''' + ' || t.descricao) as NCFOP from tcfop t';
    Open;

    while not Eof do
    begin
      I := FieldByName('CFOP').AsInteger;
{      S := Copy(S,0,1);
      I := StrToInt(S);}
      NomeNode := IntToStr((I div 100) * 100);
      if NodeExist(NomeNode) then
      begin
        Rtemp := JvTreeView1.Items[RetornaIndexNode(NomeNode)];
        JvTreeView1.Items.AddChild(Rtemp,FieldByName('NCFOP').AsString);
      end
      else
      begin
        case ((StrToInt(NomeNode) div 1000)) of
          1 : R := R1;
          2 : R := R2;
          3 : R := R3;
          5 : R := R4;
          6 : R := R5;
          7 : R := R6;
        end;
        JvTreeView1.Items.AddChild(R,NomeNode);
        Rtemp := JvTreeView1.Items[RetornaIndexNode(NomeNode)];
        JvTreeView1.Items.AddChild(Rtemp,FieldByName('NCFOP').AsString);
      end;

      {case i of
        1 : R := R1;
        2 : R := R2;
        3 : R := R3;
        5 : R := R4;
        6 : R := R5;
        7 : R := R6;
      end;}
      //JvTreeView1.Items.AddChild(R,FieldByName('NCFOP').AsString);
      Next;
    end;

    end;
end;

function TFrmCFOP.RetornaIndexNode(Texto: string): Integer;
var i : Integer;
begin
  i := 0;
  for i := 0 to JvTreeView1.Items.Count - 1 do
  begin
    if JvTreeView1.Items[i].Text = Texto then
      begin
        Result := i;
        Break;
      end;
  end;


end;

function TFrmCFOP.RetornaIndexNodeCFOP(CFOP4Digitos: string): Integer;
var i : integer;

begin
 i := 0;
  for i := 0 to JvTreeView1.Items.Count - 1 do
  begin
    if (Copy(JvTreeView1.Items[i].Text,0,4) =  CFOP4Digitos) then
      begin
        Result := i;
        Break;
      end;
  end;
end;

end.
